<template>
  <div class="gift-management-container">
    <vab-list-notice :content-array="contentArray" />
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form inline :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              clearable
              placeholder="请输入礼物名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
        <el-button :icon="Plus" type="primary" @click="handleAdd">
          添加
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="序号" prop="id" width="55" />
      <el-table-column
        align="center"
        label="礼物名称"
        prop="name"
        show-overflow-tooltip
      />

      <el-table-column align="center" label="礼物图片" show-overflow-tooltip>
        <template #default="{ row }">
          <el-image
            :preview-src-list="imageList"
            preview-teleported
            :src="row.url"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="所需积分"
        prop="fee"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="奖励积分"
        prop="income"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="created_at"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" show-overflow-tooltip>
        <template #default="{ row }">
          <el-button text type="primary" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button text type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <GiftDrawer ref="editRef" @fetch-data="fetchData" />
  </div>
</template>
<script>
  import VabListNotice from '@/plugins/VabListNotice'
  import GiftDrawer from './components/giftDrawer'
  import { doDelete, getList } from '@/api/gift'
  import { Delete, Search, Plus } from '@element-plus/icons-vue'
  import { reactive } from 'vue'
  export default defineComponent({
    name: 'GiftList',
    components: {
      VabListNotice,
      GiftDrawer,
    },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        editRef: null,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          name: '',
          page: 1,
          pageSize: 10,
        },
        contentArray: [
          '“送礼物”能够大大增加平台会员之间的互动性，提高平台会员的粘性；赠送方需要通过购买金币，才能赠送礼物，实现平台盈利；获赠方自动活动礼物对应积分，可以用来在平台兑换礼品；',
        ],
      })

      const handleAdd = () => {
        state['editRef'].showEdit()
      }

      const handleEdit = (row) => {
        state['editRef'].showEdit(row)
      }

      const handleDelete = (row) => {
        $baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ id: row.id })
          $baseMessage(msg, 'success', 'vab-hey-message-success')
          await fetchData()
        })
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.page = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.page = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        const imageList = []
        list.forEach((item) => {
          imageList.push(item.url)
        })
        state.imageList = imageList
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleAdd,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Plus,
        Delete,
        Search,
      }
    },
  })
</script>
<style lang="scss" scoped>
  .page-search {
    .el-select {
      width: 212px;
    }

    .el-input {
      width: 212px;
      border-left: 1px solid #e2ecfa;
      border-radius: 30px;
    }
  }
</style>
